package com.ruoyi.web.controller.warning;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.warning.domain.entity.Contacts;
import com.ruoyi.warning.domain.query.ContactsQuery;
import com.ruoyi.warning.service.IContactsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/warning/contacts")
@Api(tags = "联系人管理", description = "用于处理联系人相关操作的控制器")
public class ContactsController {
    @Resource
    private IContactsService iContactsService;

    @ApiOperation("联系人分页")
    @Log(title = "查询联系人", businessType = BusinessType.SELECT)
    //@PreAuthorize("@ss.hasPermi('system:config:export')")
    @GetMapping(value = "/page")
    public R<TableDataInfo> page(ContactsQuery query)
    {
        return R.ok(iContactsService.getContactsPage(query));
    }

    @ApiOperation("联系人列表")
    @Log(title = "查询联系人", businessType = BusinessType.SELECT)
    //@PreAuthorize("@ss.hasPermi('system:config:export')")
    @GetMapping(value = "/list")
    public R list(ContactsQuery query)
    {
        return R.ok(iContactsService.getContactsList(query));
    }

    @ApiOperation("详情")
    @Log(title = "联系人详情", businessType = BusinessType.SELECT)
    //@PreAuthorize("@ss.hasPermi('system:config:export')")
    @GetMapping(value = "/details/{id}")
    public R<Contacts> getDetails(@PathVariable Long id)
    {
        return R.ok(iContactsService.getDetails(id));
    }

    @ApiOperation("新增联系人")
    @Log(title = "新增联系人", businessType = BusinessType.INSERT)
    //@PreAuthorize("@ss.hasPermi('system:config:export')")
    @PostMapping(value = "/save")
    public R saveContacts(@RequestBody Contacts contacts)
    {
        System.out.println(contacts.toString());
        return R.ok(iContactsService.saveContacts(contacts));
//        return R.ok();
    }

    @ApiOperation("删除联系人")
    @Log(title = "删除联系人", businessType = BusinessType.DELETE)
    //@PreAuthorize("@ss.hasPermi('system:config:export')")
    @PostMapping (value = "/delete")
    public R deleteContacts(@RequestBody List<String> ids)
    {
        return R.ok(iContactsService.deleteContacts(ids));
    }

    @ApiOperation("修改联系人")
    @Log(title = "修改联系人", businessType = BusinessType.UPDATE)
    //@PreAuthorize("@ss.hasPermi('system:config:export')")
    @PutMapping (value = "/update")
    public R updateContacts(@RequestBody Contacts contacts)
    {
        return R.ok(iContactsService.updateContacts(contacts));
    }

    @ApiOperation("修改联系人状态")
    @Log(title = "修改联系人状态", businessType = BusinessType.UPDATE)
    //@PreAuthorize("@ss.hasPermi('system:config:export')")
    @PutMapping (value = "/updateContactStatus")
    public R updateContactStatus(@RequestBody Contacts contacts)
    {
        return R.ok(iContactsService.updateContactStatus(contacts));
    }

    @ApiOperation("导出")
    @Log(title = "导出联系人", businessType = BusinessType.OTHER)
//    @PreAuthorize("@ss.hasPermi('system:role:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response)
    {
        List<Contacts> list = iContactsService.list();
        System.out.println(list.toString());
        ExcelUtil<Contacts> util = new ExcelUtil<>(Contacts.class);
        util.exportExcel(response, list, "联系人表单");
        //iContactsService.export(response);
    }

    @ApiOperation("导入")
    @Log(title = "导入联系人", businessType = BusinessType.OTHER)
//    @PreAuthorize("@ss.hasPermi('system:user:import')")
    @PostMapping("/importData")
    public R importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<Contacts> util = new ExcelUtil<Contacts>(Contacts.class);
        List<Contacts> userList = util.importExcel(file.getInputStream());
//        String operName = getUsername();
//        String message = userService.importUser(userList, updateSupport, operName);
//        return success(message);
        return R.ok();
    }

}
